package com.jiyuanime.ui;

import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectUtil;
import com.intellij.openapi.util.Comparing;
import com.intellij.ui.ColorPanel;
import com.intellij.ui.NumberDocument;
import com.jiyuanime.ActivatePowerModeManage;
import com.jiyuanime.config.Config;
import com.jiyuanime.model.PowerMode;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * 设置
 */
public class Setting implements Configurable {

    /**
     * 每次生成的粒子量
     */
    private JTextField particleMaxCountTextField;
    /**
     * 颜色选择器
     */
    private ColorPanel colorChooser;
    /**
     * 根面板
     */
    private JPanel rootPanel;
    /**
     * 粒子颜色自动
     */
    private JCheckBox colorAutoCheckBox;
    /**
     * 插件禁用开关
     */
    private JCheckBox disabledCheckBox;
    /**
     * 彩色粒子开关
     */
    private JCheckBox colorfulCheckBox;
    /**
     * combo开关
     */
    private JCheckBox comboCheckBox;
    /**
     * 粒子开关
     */
    private JCheckBox particleCheckBox;
    /**
     * 震动开关
     */
    private JCheckBox shakeCheckBox;

    private PowerMode powerMode = Config.getInstance().powerMode;

    @Nls
    @Override
    public String getDisplayName() {
        return "Activate Power Mode";
    }

    @Nullable
    @Override
    public JComponent createComponent() {

        initListener();

        initSetting();

        return this.rootPanel;
    }

    @Override
    public boolean isModified() {
        // 是否修改
        boolean isEnable = !Comparing.equal(powerMode.isEnable, disabledCheckBox.isSelected());
        boolean isSpark = !Comparing.equal(powerMode.isSpark, particleCheckBox.isSelected());
        boolean isCombo = !Comparing.equal(powerMode.isCombo, comboCheckBox.isSelected());
        boolean isShake = !Comparing.equal(powerMode.isShake, shakeCheckBox.isSelected());
        boolean particleMaxCount = !Comparing.equal(powerMode.particleMaxCount, Integer.parseInt(particleMaxCountTextField.getText()));
        boolean particleColor = !Comparing.equal(powerMode.particleColor, colorAutoCheckBox.isSelected() ? null : colorChooser.getSelectedColor());
        return isEnable || isSpark || isCombo || isShake || particleMaxCount || particleColor;
    }

    @Override
    public void apply() throws ConfigurationException {
        // 插件开关
        powerMode.isEnable = disabledCheckBox.isSelected();
        // 彩色粒子开关
        powerMode.isColorful = colorfulCheckBox.isSelected();
        // 粒子开关
        powerMode.isSpark = particleCheckBox.isSelected();
        // combo开关
        powerMode.isCombo = comboCheckBox.isSelected();
        // 整栋开关
        powerMode.isShake = shakeCheckBox.isSelected();
        try {
            // 粒子数量
            int particleMaxCount = Integer.parseInt(particleMaxCountTextField.getText());
            if (particleMaxCount < 0) {
                throw new ConfigurationException("The 'particle max count' field must be greater than 0");
            }
            powerMode.particleMaxCount = particleMaxCount;
        } catch (NumberFormatException ex) {
            throw new ConfigurationException("The 'particle max count' field format error.");
        }
        // 颜色自动开关
        if (!colorAutoCheckBox.isSelected() && colorChooser.getSelectedColor() == null) {
            throw new ConfigurationException("'particle color' is not choose.'");
        }
        powerMode.particleColor = colorAutoCheckBox.isSelected() ? null : colorChooser.getSelectedColor();
    }

    /**
     * 初始化监听器
     */
    private void initListener() {
        disabledCheckBox.addItemListener(e -> {
            JCheckBox item = (JCheckBox) e.getItem();
            if (item.isSelected()) {
                Project project = ProjectUtil.guessCurrentProject(item);
                ActivatePowerModeManage.getInstance().init(project);
            } else {
                ActivatePowerModeManage.getInstance().destroyAll();
            }

        });
        colorAutoCheckBox.addItemListener(e -> {
            JCheckBox item = (JCheckBox) e.getItem();
            colorChooser.setSelectedColor(null);
            colorChooser.setEditable(!item.isSelected());
        });
    }

    /**
     * 初始化设置
     */
    private void initSetting() {
        // 插件开关
        disabledCheckBox.setSelected(powerMode.isEnable);
        // 彩色粒子开关
        colorfulCheckBox.setSelected(powerMode.isColorful);
        // 粒子开关
        particleCheckBox.setSelected(powerMode.isSpark);
        // combo开关
        comboCheckBox.setSelected(powerMode.isCombo);
        // 震动开关
        shakeCheckBox.setSelected(powerMode.isShake);
        // 限制只能输入数字
        particleMaxCountTextField.setDocument(new NumberDocument());
        // 粒子数量
        particleMaxCountTextField.setText(String.valueOf(powerMode.particleMaxCount));
        // 颜色自动开关
        if (powerMode.particleColor == null) {
            colorAutoCheckBox.setSelected(true);
        } else {
            colorChooser.setSelectedColor(powerMode.particleColor);
        }
    }
}
